package name.jiaosha.air.system.controller;

import java.util.ArrayList;
import java.util.List;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.system.domain.Seer;
import name.jiaosha.air.system.service.ISystemService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.PageInfo;

/**
 * 能效控制器
 * 
 * @author xu
 * 
 */
@Controller
@RequestMapping("/admin/sys")
public class SeerController {

	@Autowired
	private ISystemService systemService;

	@RequestMapping("/seerList")
	public ModelAndView getUnitList() {

		PageInfo<Seer> pageInfo = null;

		if (!AppConstants.DEBUG) {
			List<Seer> list = new ArrayList<Seer>();
			for (int i = 1; i <= 20; i++) {
				Seer seer = new Seer();
				seer.setId(i);
				seer.setName("国家" + i + "级");
				list.add(seer);
			}

		} else {

			pageInfo = systemService.getSeerListByComId(1,
					AppConstants.DEFAULT_SIZE);

		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", pageInfo.getItemList());
		model.addAttribute("totalProperty", pageInfo.getTotalProperty());
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/createSeer")
	public ModelAndView createUnit(@RequestParam String name) {

		if (!AppConstants.DEBUG) {

		} else {

			Seer seer = new Seer();
			seer.setName(name.trim());
			systemService.createSeer(seer);

		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");

		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/updateSeer")
	public ModelAndView updateUnit(@RequestParam int id,
			@RequestParam String name) {

		if (!AppConstants.DEBUG) {

		} else {

			Seer seer = systemService.getSeerById(id);

			if (seer != null) {

				seer.setName(name.trim());
				systemService.updateSeer(seer);
			}

		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

}
